package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/travel")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") TravelQueryObject qo, Model model) {
        //如果状态没有值,默认查询待审核状态
        if (qo.getState() == null) {
            qo.setState(Travel.STATE_WAIT);
        }
        //按照最近更新时间升序排列
        qo.setOrderBy("t.lastUpdateTime");
        //必须是公开状态的游记
        qo.setIsPublic(true);
        model.addAttribute("pageInfo", travelService.queryTravel(qo));
        return "travel/list";
    }

    @RequestMapping(value = "/getContent", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public Object getContent(Long id) {
        return travelService.getContent(id);
    }

    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(Long id, Integer state) {
        travelService.changeState(id, state);
        return new JSONResult();
    }
    //尝试合并!
}
